package com.dormitory_springboot.controller;


import com.dormitory_springboot.entity.Utilities;
import com.dormitory_springboot.form.SearchForm;
import com.dormitory_springboot.service.UtilitiesService;
import com.dormitory_springboot.util.ResultVOUtil;
import com.dormitory_springboot.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-01-08
 */
@RestController
@RequestMapping("/utilities")
public class UtilitiesController {
    @Autowired
    private UtilitiesService utilitiesService;

    @PostMapping("/save")
    public ResultVO save(@RequestBody Utilities utilities) {
        boolean save = this.utilitiesService.save(utilities);
        if (!save) return ResultVOUtil.fail("保存失败");
        return ResultVOUtil.success(null);
    }

    @GetMapping("/list/{page}/{size}")
    public ResultVO list(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        return ResultVOUtil.success(this.utilitiesService.list(page, size));
    }

    @GetMapping("/search")
    public ResultVO search(SearchForm searchForm) {
        return ResultVOUtil.success(this.utilitiesService.search(searchForm));
    }

    @GetMapping("/findById/{id}")
    public ResultVO findById(@PathVariable("id") Integer id) {
        return ResultVOUtil.success(this.utilitiesService.getById(id));
    }

    @PutMapping("/update")
    public ResultVO update(@RequestBody Utilities utilities) {
        boolean update = this.utilitiesService.updateById(utilities);
        if (!update) return ResultVOUtil.fail("更新失败");
        return ResultVOUtil.success(null);
    }

    @DeleteMapping("/deleteById/{id}")
    public ResultVO deleteById(@PathVariable("id") Integer id) {
        Boolean delete = this.utilitiesService.removeById(id);
        if (!delete) return ResultVOUtil.fail("删除失败");
        return ResultVOUtil.success(null);
    }

    @GetMapping("/getInfoByStudentId/{studentId}")
    public ResultVO getWaterElectricityInfoByStudentId(@PathVariable("studentId") Integer studentId) {
        return ResultVOUtil.success(this.utilitiesService.getWaterElectricityInfoByStudentId(studentId));
    }

}

